package tedexis.mercurio.transformers;

import org.apache.log4j.Logger;

import gurulab.maya.common.PhoneAddress;
import gurulab.merlin.core.envelope.Envelope;
import gurulab.merlin.core.router.EnvelopeTransformer;

public class DigitelTransformer implements EnvelopeTransformer {

	private Logger logger = Logger.getLogger (DigitelTransformer.class);
	private final String PEER = "digitel";
	public void transform(Envelope envelope) {
		
		
		if(envelope.getDestinationRoute()!=null && envelope.getDestinationRoute().getPeer().equalsIgnoreCase(PEER)){
			//Va hacia la operadora
			//Transformacion del SMPPOperator
			PhoneAddress phoneAdd = (PhoneAddress)envelope.getDestination ();
			phoneAdd.setCrude (false);
			phoneAdd.setSchema("phone");
			phoneAdd.setNumber (phoneAdd.getCountryCode ()+phoneAdd.getAreaCode ()+phoneAdd.getLocal ());
			envelope.setDestination (phoneAdd);
		}
		
		if(envelope.getOriginRoute()!=null && envelope.getOriginRoute().getPeer().equalsIgnoreCase(PEER)){
			//Recibe de la operadora
			//Transformacion del SmppEP
			PhoneAddress phoneAdd = (PhoneAddress)envelope.getOrigin();
			phoneAdd.setCrude (true);
			phoneAdd.setSchema("phone");
			phoneAdd.setDomain("58");
			phoneAdd.setPort(phoneAdd.getNumber().substring(0, 3));
			phoneAdd.setLocal(phoneAdd.getNumber().substring(3));
			//phoneAdd.setPort(phoneAdd.getNumber().substring(2, 3));
			//phoneAdd.setLocal(phoneAdd.getNumber().substring(5));
			envelope.setOrigin(phoneAdd);
		}
		logger.info ("Envelope=" + envelope.toStringNoContent ());
	}
	
}
